<?php
class PayAction extends CommonAction {

    public function index()
    {
        $pay = M('Pay');

        $payData = $pay->find();

        if(isset($_POST['send']))
        {
            $this->validate();
            $data['native'] = $this->_post('native');
            $data['foreign'] = $this->_post('foreign');

            if(sizeof($payData) != 0)
            {
                $data['id'] = $payData['id'];
                $pay->save($data);
            }
            else
            {
                $pay->add($data);
            }

            redirectByModal('更新就餐费用成功!', self::$SUCCESS, __URL__ . '/index');
        }

        $this->assign(
            array(
                'native' => $payData['native'],
                'foreign' => $payData['foreign'],
            )
        );

        $this->display();
    }

    private function validate()
    {
        $native = $this->_post('native');
        $foreign = $this->_post('foreign');

        if($native == '')
            redirectByModal('本单位就餐费用必须填写!', self::$ERROR);
        else if(!is_numeric($native))
            redirectByModal('本单位就餐费用必须是数字!', self::$ERROR);
        if($foreign == '')
            redirectByModal('外单位就餐费用必须填写!', self::$ERROR);
        else if(!is_numeric($foreign))
            redirectByModal('外单位就餐费用必须是数字!', self::$ERROR);
    }
}